var stepNo=0, 
testNum=192,
type_one_num=180,
groupNum=30;

function progress() {
	stepNo=stepNo+1;
	perc = parseInt(stepNo/testNum*100);
	perc = perc >= 100 ? 100 : perc;
	$('.progress-bar').css('width', perc+'%');
	$('.dkjohn-percent').data('percent', perc);
	$('.dkjohn-percent').html(perc+'%');
	$('.dkjohn-percent').css('position','relative');
}

function calc(pos) {
	var value = 0, 
		start = pos * groupNum + 1,
		end = start + groupNum;
	for(var i=start; i<end; i++) {
		var checkValue = $('input[name="test'+i+'"]:checked').val();
		value += parseInt(checkValue);
	}
	return value;
}

function setScore() {
	var scoreR = parseInt($('input[name="A_R"]:checked').val()) + parseInt($('input[name="B_R"]:checked').val());
	var scorI = parseInt($('input[name="A_I"]:checked').val()) + parseInt($('input[name="B_I"]:checked').val());
	var scoreA = parseInt($('input[name="A_A"]:checked').val()) + parseInt($('input[name="B_A"]:checked').val());
	var scoreS = parseInt($('input[name="A_S"]:checked').val()) + parseInt($('input[name="B_S"]:checked').val());
	var scoreE = parseInt($('input[name="A_E"]:checked').val()) + parseInt($('input[name="B_E"]:checked').val());
	var scoreC = parseInt($('input[name="A_C"]:checked').val()) + parseInt($('input[name="B_C"]:checked').val());
	$("input[name='total_r']").val(calc(0) + scoreR);
	$("input[name='total_i']").val(calc(1) + scorI);
	$("input[name='total_a']").val(calc(2) + scoreA);
	$("input[name='total_s']").val(calc(3) + scoreS);
	$("input[name='total_e']").val(calc(4) + scoreE);
	$("input[name='total_c']").val(calc(5) + scoreC);
}

//开始答题
$('.dkjohn-tip-start').click(function() {
	$('.dkjohn-tip').addClass('hidden');
	$('.dkjohn-test-stage2').removeClass('hidden');
	$('.dkjohn-test1').removeClass('hidden');
});

//下一题
$('.dkjohn-next').bind('click', function(){
	var curId = $(this).attr('id');
	var curPos =parseInt(curId.substr(0, curId.length-1));
	var nextPos = curPos + 1;
	$(this).parent().parent().find('input').attr('disabled', 'disabled');
	progress();
	setTimeout(function() {
		$('.dkjohn-test'+curPos).fadeOut(function() {
				if(curPos<=testNum){
					if(curPos == type_one_num) {
						$('.dkjohn-tech1').removeClass('hidden');
						return;
					}
					$('.dkjohn-test'+nextPos).removeClass('hidden');
				}
			}
		);
	},500);
});
//能力类型选择
$('.dkjohn-tech-select').bind('click', function(){
	var ul = $(this).parent().parent();
	var div = ul.parent();
	var content = ul.parent().parent();
	var checkNum = 0;

	if(typeof(ul.data('do')) == 'undefined') {
		ul.attr('data-do', 'cheked');
		progress();
		div.removeClass('techbg')
	}
	
	content.find('ul').each(function(i){
		if(typeof($(this).data('do')) != 'undefined') {
			checkNum ++;
		}
	});

	if(checkNum == content.find('ul').length) {
		setTimeout(function() {
			content.fadeOut(function() {
				$(content.next()).removeClass('hidden');
				if($('.dkjohn-percent').data('percent') == '100') {
					$('.progress').addClass('hidden');
					$('.dkjohn-verify').removeClass('hidden');
				}
			});
		},500);	
	}
});

$('#subtest').click(function() {
	setScore();
});

$('#form-stage2').ajaxForm({
	url: basepath + '/contoller/save.do',
	type: 'POST',
	beforeSubmit: function(arr) {
		$('button[type="submit"]').button('loading');
		return true;
	},
	success: function(data, status) {
		if(data){
			location.href= basepath + data;
		}
		
	},
	error: function(xhr, status) {
		$('button[type="submit"]').button('reset');
		errorMsg($('#form-stage2'), '网络错误。('+status+')', 'form-group');
		$('.alert').addClass('col-sm-6 col-sm-offset-3');
    }
});


